<template>
  <!-- <div class="container" style="width: 100%; height: 100%;"> -->
  <el-card class="box-card">
    <div slot="header" class="clearfix">
      <span class="active">缴费</span>
    </div>

    <el-row>
      <!-- 搜索部分 -->
      <el-col :span="24">
        <el-form :inline="true" :model="query" class="demo-form-inline" ref="queryForm">
          <el-form-item label="状态">
            <el-select style="width: 100px;" v-model="query.state" clearable>
              <el-option label="未缴费" :value="1"></el-option>
              <el-option label="已缴费" :value="2"></el-option>
            </el-select>
          </el-form-item>
          
          <el-form-item label="账期">
            <el-date-picker
             style="width: 200px;"
              v-model="dateRange"
              type="daterange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              value-format="yyyy-MM-dd">
            </el-date-picker>
          </el-form-item>
          
          <el-form-item label="楼栋">
            <CasCader style="width: 200px;" @handleChange="handleChange"></CasCader>
          </el-form-item>
          
          <el-form-item label="房号">
            <el-input style="width: 120px;" v-model="query.houseno"  clearable></el-input>
          </el-form-item>
          
          <el-form-item label="姓名">
            <el-input style="width: 120px;" v-model="query.userName"  clearable></el-input>
          </el-form-item>
          
          <el-form-item label="电话">
            <el-input style="width: 120px;" v-model="query.mobile"  clearable></el-input>
          </el-form-item>
          
          <el-form-item label="添加人">
            <el-input style="width: 180px;" v-model="query.admin"  clearable></el-input>
          </el-form-item>
          
          <el-form-item label="流水账单">
            <el-input style="width: 180px;" v-model="query.orderno"  clearable></el-input>
          </el-form-item>
          
          <el-form-item>
            <el-button type="primary" @click="search">查询</el-button>
            <el-button @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>

    <el-row :gutter="10" class="btns">
      <el-button type="primary">添加</el-button>
      <el-button type="primary" class="ico" size="small" @click="handleDeleteAll">批量删除</el-button>
    </el-row>

    <!--  表格 -->

    <BaseTable
      :tableColumn="tableColumn"
      :tableData="tableData"
      :loading="loading"
      @edit="handleClick"
      @delete="handleDelete"
      :isShowSelection="isShowSelection"
      @selection-change="handleSelectionChange"
    >
      <template #options="{ row }">
        <el-button @click="handleEdit(row)" type="success" size="mini">修改</el-button>
        <el-button size="mini" @click="handleDelete(row)" type="danger">删除</el-button>
      </template>
    </BaseTable>

    <!-- 分页 -->
    <el-pagination
      class="pagination"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="query.page"
      :page-sizes="[5, 10, 20, 50]"
      :page-size="query.psize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="counts"
    >
    </el-pagination>

    <!-- 添加/编辑弹窗 -->
    <DialogHouse :dialogVisible="dialogVisible" :isEdit="isEdit" :editData="editData" @handleCancel="handleCancel" @refreshList="refreshList">
    </DialogHouse>
  </el-card>
  <!-- </div> -->
</template>

<script>
import CasCader from '../components/CasCader.vue';
import DialogHouse from '../components/DialogHouse.vue'
import { list, delAll, del } from '../api/fee'
import BaseTable from '@/components/BaseTable.vue'
import { miXinPower } from '@/utils/miXinPower.js'

export default {
  name: 'FeeView',
  components: {
    CasCader,
    DialogHouse,
    BaseTable,
  },
  mixins: [miXinPower],
  data() {
    return {
      dialogVisible: false,
      isEdit: false,
      editData: {},
      orientationDat: ['朝南', '朝东', '朝北', '朝西'],
      loading: true,
      query: {
        page: 1,
        psize: 5,
        key: '',
        type: '',
        state: null,
        begindate: '',
        enddate: '',
        buildingid: '',
        houseno: '',
        userName: '',
        mobile: '',
        admin: '',
        orderno: ''
      },
      dateRange: [],
      isShowSelection: true,
      counts: 0,
      selectedRows: [],
      tableColumn: [
        {
          prop: 'building',
          label: '楼号号',
          width: 180,
        },
        {
          prop: 'houseno',
          label: '房间号',
        },
        {
          prop: 'userName',
          label: '姓名',
        },
        {
          prop: 'mobile',
          label: '联系方式',
        },
        {
          prop: 'type',
          label: '缴费类型',
        },
        {
          prop: 'begindate',
          label: '账期',
        },
        {
          prop: 'price',
          label: '应缴金源',
        },
        {
          prop: 'stateName',
          label: '状态',
        },
        {
          prop: 'addtime',
          label: '导入时间',
        },
        {
          prop: 'admin',
          label: '添加人',
        },
        {
          prop: 'options',
          label: '操作',
          slotname: 'options',
        },
      ],
      gridData: [],
      dialogTableVisible: false,
      dialogFormVisible: false,
      form: {
        name: '',
        region: '',
        date1: '',
        date2: '',
        delivery: false,
        type: [],
        resource: '',
        desc: '',
      },
      formLabelWidth: '120px',

      currentPage1: 5,
      currentPage2: 5,
      currentPage3: 5,
      currentPage4: 4,
      formInline: {
        user: '',
        region: '',
      },
      tableData: [],
      multipleSelection: [],
    }
  },

  methods: {
    handleAdd() {
      this.isEdit = false
      this.dialogVisible = true
    },

    refreshList() {
      this.dialogVisible = false
      this.isEdit = false
      this.getList()
    },

    handleCancel() {
      this.dialogVisible = false
      this.isEdit = false
    },

    handleChange(buildingarr) {
      this.query.buildingid = buildingarr.length ? buildingarr[buildingarr.length - 1] : ''
    },
    
    handleEdit(row) {
      this.isEdit = true
      this.editData = { ...row }
      this.dialogVisible = true
    },
    
    handleSelectionChange(selectedRows) {
      this.selectedRows = selectedRows
    },
    
    handleDeleteAll() {
      if (!this.selectedRows.length) return this.$message.error('请选择要删除的记录')
      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(async () => {
          let ids = this.selectedRows.map((item) => item.id)
          await delAll({ ids: ids })
          await this.getList()
          this.$message.success('删除成功')
        })
        .catch(() => {
          this.$message.info('已取消删除')
        })
    },
    
    handleDelete(row) {
      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(async () => {
          await del({ id: row.id })
          await this.getList()
          this.$message.success('删除成功')
        })
        .catch(() => {
          this.$message.info('已取消删除')
        })
    },
    
    handleClick() {},

    async getList() {
      this.loading = true
      try {
        // 处理日期范围
        if (this.dateRange && this.dateRange.length === 2) {
          this.query.begindate = this.dateRange[0]
          this.query.enddate = this.dateRange[1]
        } else {
          this.query.begindate = ''
          this.query.enddate = ''
        }
        
        let {
          data: { counts, list: listData },
        } = await list(this.query)
        this.counts = counts
        this.tableData = listData
        this.loading = false
      } catch (error) {
        console.error('获取数据失败:', error)
        this.loading = false
      }
    },
    
    onSubmit() {},
    
    handleSizeChange(val) {
      this.query.psize = val
      this.query.page = 1
      this.getList()
    },
    
    handleCurrentChange(val) {
      this.query.page = val
      this.getList()
    },
    
    search() {
      this.query.page = 1
      this.getList()
    },
    
    // 重置查询条件
    resetQuery() {
      this.$refs.queryForm.resetFields()
      this.dateRange = []
      this.query.buildingid = ''
      this.query.page = 1
      this.getList()
    }
  },

  created() {
    this.getList()
  },
}
</script>

<style lang="less" scoped>
@import url(../assets/css/search.less);

.ico {
  height: 40px;
  background-color: #f56c6c;
}
</style>